package com.explorer.ider.bootguide.fragment;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.explorer.ider.bootguide.MainActivity;
import com.explorer.ider.bootguide.R;
import com.explorer.ider.bootguide.utils.DisplayPositionManager;
import com.explorer.ider.bootguide.utils.NetUtil;


/**
 * Created by Eric on 2016/9/5.
 */
public class FragmentScreenPosition extends Fragment{

    private DisplayPositionManager mDisPlayPositionManager;
    private View views;
    private TextView mScreenInZoomButton;
    private TextView mScreenOutZoomButton;
    private Button mScreenSkipButton,mScreenBackButton;
    private CardView mSelectLockCardView;
    private ImageView mSelectPositionTVImageView;
    private Toast mToast;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor editor;
    private int onPosition = 100;

    private static final String TAG = "FragmentScreenPosition";


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (views == null){
            views = inflater.inflate(R.layout.fragment_screen_position, container,false);
            initData();
            initView();
        }
        return views;
    }

    private void initData() {
        if(MainActivity.DEVICE_NAME.equals("rk322x_box") || MainActivity.DEVICE_NAME.equals("m201")) {
            mPreferences = getActivity().getSharedPreferences("screen_position", Context.MODE_PRIVATE);
            editor = mPreferences.edit();
            editor.putInt("position",100);
            editor.commit();
        }else {
            mDisPlayPositionManager = new DisplayPositionManager(getActivity());
        }
    }

    private void initView() {
        final Animation mAnimationIn , mAnimationOut;
        mAnimationIn = AnimationUtils.loadAnimation(getActivity(), R.anim.view_focus_animation_in);
        mAnimationOut = AnimationUtils.loadAnimation(getActivity(),R.anim.view_focus_animation_out);
        mAnimationIn.setFillAfter(true);
        mAnimationOut.setFillAfter(true);
        mScreenInZoomButton = (TextView) views.findViewById(R.id.id_zoom_in_sreen_button);
        mScreenOutZoomButton = (TextView) views.findViewById(R.id.id_zoom_out_sreen_button);
        mScreenSkipButton = (Button) views.findViewById(R.id.id_screen_scale_skip_button);
        mScreenBackButton = (Button) views.findViewById(R.id.id_screen_scale_back_button);
        mSelectLockCardView = (CardView) views.findViewById(R.id.id_zoom_select_focus_lock_card_view);
        mSelectPositionTVImageView = (ImageView) views.findViewById(R.id.id_screen_scale_position_tv_image);
        mScreenInZoomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MainActivity.DEVICE_NAME.equals("rk322x_box") || MainActivity.DEVICE_NAME.equals("m201")) {
                    try {
                        Intent intent = new Intent();
                        intent.setAction(MainActivity.ACTION_SCREEN_ENLARGE);
                        Log.i(TAG, "onClick: " + intent.getAction());
                        getActivity().sendBroadcast(intent);
                        onPosition = mPreferences.getInt("position", 0);
                        onPosition += 10;
                        editor.putInt("position", onPosition);
                        editor.commit();
                        Toast.makeText(getActivity(), getString(R.string.string_screen_zoom_current_rate_value) +
                                onPosition + "%", Toast.LENGTH_SHORT).show();
                    }catch (NullPointerException e){

                    }
                }else {
                    showToast(getString(R.string.string_screen_zoom_current_rate_value) +
                            mDisPlayPositionManager.getCurrentRateValue() + "%");
                    mDisPlayPositionManager.zoomIn();

                }
                startAnimation(view);
            }
        });
        mScreenOutZoomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MainActivity.DEVICE_NAME.equals("rk322x_box") || MainActivity.DEVICE_NAME.equals("m201")) {
                    try{
                        Intent intent = new Intent();
                        intent.setAction(MainActivity.ACTION_SCREEN_NARROW);
                        Log.i(TAG, "onClick: " + intent.getAction());
                        getActivity().sendBroadcast(intent);
                        onPosition = mPreferences.getInt("position",0);
                        onPosition -= 10;
                        editor.putInt("position",onPosition);
                        editor.commit();
                        Toast.makeText(getActivity(), getString(R.string.string_screen_zoom_current_rate_value) +
                                onPosition + "%", Toast.LENGTH_SHORT).show();
                    }catch (NullPointerException e){

                    }
                }else {
                    showToast(getString(R.string.string_screen_zoom_current_rate_value) +
                            mDisPlayPositionManager.getCurrentRateValue() + "%");
                    mDisPlayPositionManager.zoomOut();
                }
                startAnimation(view);
            }
        });

        mScreenInZoomButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                startAnimation(view);
            }
        });
        mScreenOutZoomButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                startAnimation(view);
            }
        });
        mScreenSkipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                if (NetUtil.isNetworkAvailable(getActivity())) {
                    intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER4);
                }else {
                    intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER4);
                    //intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER5);
                }
                getActivity().sendBroadcast(intent);
            }
        });
        mScreenSkipButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                startAnimation(v);

                if (hasFocus) {
//                    v.startAnimation(mAnimationIn);
//                    mSelectLockCardView.setVisibility(View.INVISIBLE);
                }else {
//                    v.startAnimation(mAnimationOut);
//                    mSelectLockCardView.setVisibility(View.VISIBLE);
                }
            }
        });
        mScreenBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
//                intent.setAction(MainActivity.ACTION_TO_NEXT_PAGER5);
                getActivity().sendBroadcast(intent);
            }
        });
        mScreenBackButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                startAnimation(v);
                if (hasFocus) {
//                    v.startAnimation(mAnimationIn);
//                    mSelectLockCardView.setVisibility(View.INVISIBLE);
                }else {
//                    v.startAnimation(mAnimationOut);
//                    mSelectLockCardView.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    Handler screenPositionHandler = new Handler(){
        @Override
        public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
            return super.sendMessageAtTime(msg, uptimeMillis);
        }
    };

    public void startAnimation(View v){
        final ValueAnimator animator = ValueAnimator.ofInt((int)mSelectLockCardView.getY(),(int)v.getY()+3);
        animator.setDuration(200);
        animator.setInterpolator(new LinearInterpolator());//线性效果变化
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Integer integer = (Integer) animator.getAnimatedValue();
                mSelectLockCardView.setY(integer);
            }
        });
        animator.start();
    }

    public void showToast(String text){
        if (mToast == null) {
            mToast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
            mToast.show();
        }else {
            mToast.setText(text);
            mToast.show();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!MainActivity.DEVICE_NAME.equals("rk322x_box") && !MainActivity.DEVICE_NAME.equals("m201"))
        mDisPlayPositionManager.saveDisplayPosition();
    }


}
